package com.core.web.struts2.taglib;

import java.util.*;

import org.apache.struts2.views.jsp.*;

import javax.servlet.jsp.*;

import com.core.util.*;
import com.core.cache.*;

/**
 * property 属性 JSP 标签
 * 
 * 
 */
public class PropertyTag extends CommonTag {

	private String cachename = "";

	@Override
	public int doStartTag() throws JspException {
		return SKIP_BODY;
	}

	@Override
	public int doEndTag() throws javax.servlet.jsp.JspException {
		try {
			pageContext.getOut().print(getHtml());
		} catch (Exception e) {
			throw new JspException(e);
		}
		return EVAL_BODY_INCLUDE;
	}

	private String getHtml() {
		String ret = "";

		Object stackObject = TagUtils.getStack(this.pageContext)
				.findValue(name);
		if (getValue() != null && !getValue().equals("")) {
			ret = getValue();
		}
		if ( stackObject!=null && !stackObject.equals("")) {
			ret = stackObject.toString();
		}

		/**
		 * 从通用缓存中取出相应值的显示信息
		 */
		if (this.cachename != null && !this.cachename.equals("")) {
			if (cachename.equals("nowTime")) {
				ret = DateUtil.formatLongNow();
			} else if (cachename.equals("now_long")) {
				ret = DateUtil.formatLongNow();
			} else if (cachename.equals("now_short")) {
				ret = DateUtil.formatShortNow();
			} else {
				List<Common> datas = CommonCache.getInstance().getCommon(
						this.cachename);
				if (datas != null && !datas.isEmpty()) {
					for (Common data : datas) {
						if (data.getKey().equals(ret)) {
							ret = data.getValue();
							break;
						}
					}
				}
			}
		}

		/**
		 * 展示值的格式化处理
		 */
		if (this.format != null && !this.format.equals("")) {
			String type = "";
			String style = "";

			if (this.format.indexOf(":") == -1) {
				type = this.format;
			} else {
				type = format.substring(0, format.indexOf(":"));
				style = format.substring(format.indexOf(":") + 1, format
						.length());
			}

			try {
				if (type.equals("sub") && !ret.equals("")
						&& ret.length() > Integer.parseInt(style)) {// 值的字符串截取
					ret = ret.substring(0, Integer.parseInt(style));
				} else if (type.equals("num") && !ret.equals("")) {// 值的数字格式化处理
					ret = NumberUtil.formatNumber(ret, Integer.parseInt(style));
				} else if (type.equals("view") && !ret.equals("")) {// 值的多行文本处理
					ret = StringUtil.formatMultiLineString(ret);
				} else if (type.equals("%") && !ret.equals("")) {// 值的格式百分比处理
					double temp = Double.parseDouble(NumberUtil.formatNumber(
							(new Double(ret).doubleValue() * 100) + "", Integer
									.parseInt(style)));
					if (temp == 0.00) {
						ret = "";
					} else {
						ret = (temp) + "%";
					}
				} else if (type.equals("short_date") && stackObject != null) {
					Date inputDate = (Date) stackObject;
					if (inputDate != null) {
						ret = DateUtil.formatShortDate(inputDate);
					}

				} else if (type.equals("long_date") && stackObject != null) {
					Date inputDate = (Date) stackObject;
					if (inputDate != null) {
						ret = DateUtil.formatLongDate(inputDate);
					}
				}

			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		return ret;
	}

	public String getCachename() {
		return cachename;
	}

	public void setCachename(String cachename) {
		this.cachename = cachename;
	}
}
